//
//  SongListTypeViewController.m
//  YHYRealx
//
//  Created by yanghuiyan on 16/5/3.
//  Copyright © 2016年 yanghuiyan. All rights reserved.
//

#import "SongListTypeViewController.h"
#import "SongListTypeModel.h"
#import "SongListTypeChlidrenModel.h"
#import "SongListTypeCollectionViewCell.h"
#import "SongListTypeCollectionReusableView.h"
@interface SongListTypeViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) NSMutableArray *dataSource;

@property (nonatomic, retain) NSMutableArray *arrChildren;

@property (nonatomic, retain) YHYBaseCollectionView *collectionView;

@end

@implementation SongListTypeViewController

- (void)dealloc {
    
    [_collectionView release];
    [_arrChildren release];
    [_dataSource release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithRed:0.16 green:0.20 blue:0.25 alpha:1.00];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    titleLabel.text = @"选择标签";
    titleLabel.font = [UIFont boldSystemFontOfSize:22];
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    [titleLabel release];
    
    self.dataSource = [NSMutableArray array];
    self.arrChildren = [NSMutableArray array];
    
    [self creatView];
    [self dataHandle];
    
    
}

#pragma mark - ------------ 创建视图 ---------------
- (void)creatView {
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//    flowLayout.itemSize = CGSizeMake(WIDTH / 4, 40);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectionView = [[YHYBaseCollectionView alloc] initWithFrame:CGRectMake(0, 5, WIDTH, HEIGHT - 69) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor colorWithRed:0.16 green:0.20 blue:0.25 alpha:1.00];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCellIdentifier"];
    [self.collectionView registerClass:[SongListTypeCollectionViewCell class] forCellWithReuseIdentifier:@"SongListType"];
    [self.collectionView registerClass:[SongListTypeCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"TypeReusableView"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    
}

#pragma mark - ------------ collectionView协议方法 ---------------

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    if (self.dataSource.count != 0) {
        
        return self.dataSource.count + 1;
    }
    return 0;
    
}

#pragma mark - UICollectionViewDelegateFlowLayout
// layout 布局 根据分区 返回不同的layout大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return CGSizeMake(WIDTH, 40);
    }
    return CGSizeMake(WIDTH / 4, 40);
}

#pragma mark - 返回头视图方法
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (kind == UICollectionElementKindSectionHeader) {
        
        SongListTypeCollectionReusableView *type = [self.collectionView dequeueReusableSupplementaryViewOfKind: kind withReuseIdentifier:@"TypeReusableView" forIndexPath:indexPath];
        SongListTypeModel *typeModel = self.dataSource[indexPath.section - 1];
        type.label.text = typeModel.tag;

        return type;
        
    }
    return nil;
}

#pragma mark - ------------ 头视图高度 ---------------
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    if(section == 0) {
        return CGSizeMake(0, 0);;
    }
    return CGSizeMake(WIDTH, 30);
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
   if (section == 0) {
       
        return 1;
       
    }
   if (section == 1) {
        
        NSMutableArray *arr = [NSMutableArray array];
        SongListTypeModel *typeModel = self.dataSource[section - 1];
        for (NSDictionary *dic in typeModel.children) {
            
            SongListTypeChlidrenModel *model = [[SongListTypeChlidrenModel alloc] initWithDataSource:dic];
            [arr addObject:model];
            
        }
        return arr.count;
    }
    if (section == 2) {
        
        NSMutableArray *arr = [NSMutableArray array];
        SongListTypeModel *typeModel = self.dataSource[section - 1];
        for (NSDictionary *dic in typeModel.children) {
            
            SongListTypeChlidrenModel *model = [[SongListTypeChlidrenModel alloc] initWithDataSource:dic];
            [arr addObject:model];
            
        }
        return arr.count;
    }
    if (section == 3) {
        
        NSMutableArray *arr = [NSMutableArray array];
        SongListTypeModel *typeModel = self.dataSource[section - 1];
        for (NSDictionary *dic in typeModel.children) {
            
            SongListTypeChlidrenModel *model = [[SongListTypeChlidrenModel alloc] initWithDataSource:dic];
            [arr addObject:model];
            
        }
        return arr.count;
    }
    if (section == 4) {
        
        NSMutableArray *arr = [NSMutableArray array];
        SongListTypeModel *typeModel = self.dataSource[section - 1];
        for (NSDictionary *dic in typeModel.children) {
            
            SongListTypeChlidrenModel *model = [[SongListTypeChlidrenModel alloc] initWithDataSource:dic];
            [arr addObject:model];
            
        }
        return arr.count;
    }
    if (section == 5) {
        
        NSMutableArray *arr = [NSMutableArray array];
        SongListTypeModel *typeModel = self.dataSource[section - 1];
        for (NSDictionary *dic in typeModel.children) {
            
            SongListTypeChlidrenModel *model = [[SongListTypeChlidrenModel alloc] initWithDataSource:dic];
            [arr addObject:model];
            
        }
        return arr.count;
    }


    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCellIdentifier" forIndexPath:indexPath];
        YHYBaseLabel *label = [[YHYBaseLabel alloc] initWithFrame:CGRectMake(5, 0, WIDTH - 10, 40)];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"全部";
        label.layer.borderWidth = 1.0f;
        label.font = [UIFont systemFontOfSize:16];
        label.textColor = [UIColor colorWithRed:0.49 green:0.51 blue:0.53 alpha:1.00];
        label.backgroundColor = [UIColor colorWithRed:0.13 green:0.17 blue:0.21 alpha:1.00];
        [cell addSubview:label];
        return cell;
    }
    
    if (indexPath.section == 1) {
        
        SongListTypeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SongListType" forIndexPath:indexPath];
        SongListTypeModel *typeModel = self.dataSource[indexPath.section - 1];
        for (NSDictionary *dic in typeModel.children) {
            SongListTypeChlidrenModel *model = [[SongListTypeChlidrenModel alloc] initWithDataSource:dic];
            [_arrChildren addObject:model];
        }
        cell.typeModel = _arrChildren[indexPath.item];
        return cell;

        
    }
    
    if (indexPath.section == 2) {
        
        SongListTypeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SongListType" forIndexPath:indexPath];
        SongListTypeModel *typeModel = self.dataSource[indexPath.section - 1];
        for (NSDictionary *dic in typeModel.children) {
            SongListTypeChlidrenModel *model = [[SongListTypeChlidrenModel alloc] initWithDataSource:dic];
            [_arrChildren addObject:model];
        }
        cell.typeModel = _arrChildren[indexPath.item];
        return cell;
        
        
    }
    
    if (indexPath.section == 3) {
        
        SongListTypeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SongListType" forIndexPath:indexPath];
        SongListTypeModel *typeModel = self.dataSource[indexPath.section - 1];
        for (NSDictionary *dic in typeModel.children) {
            SongListTypeChlidrenModel *model = [[SongListTypeChlidrenModel alloc] initWithDataSource:dic];
            [_arrChildren addObject:model];
        }
        cell.typeModel = _arrChildren[indexPath.item];
        return cell;
        
        
    }

    if (indexPath.section == 4) {
        
        SongListTypeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SongListType" forIndexPath:indexPath];
        SongListTypeModel *typeModel = self.dataSource[indexPath.section - 1];
        for (NSDictionary *dic in typeModel.children) {
            SongListTypeChlidrenModel *model = [[SongListTypeChlidrenModel alloc] initWithDataSource:dic];
            [_arrChildren addObject:model];
        }
        cell.typeModel = _arrChildren[indexPath.item];
        return cell;
        
        
    }

    if (indexPath.section == 5) {
        
        SongListTypeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SongListType" forIndexPath:indexPath];
        SongListTypeModel *typeModel = self.dataSource[indexPath.section - 1];
        for (NSDictionary *dic in typeModel.children) {
            SongListTypeChlidrenModel *model = [[SongListTypeChlidrenModel alloc] initWithDataSource:dic];
            [_arrChildren addObject:model];
        }
        cell.typeModel = _arrChildren[indexPath.item];
        return cell;
        
        
    }

    return nil;
    
}

#pragma mark - ------------ 数据解析 ---------------
- (void)dataHandle {
    
    [YHYAFNetTool GETNetWirhUrl:@"http://api.dongting.com/song/tag/songlist" body:nil headerFile:nil response:YHYJSON Success:^(id result) {
        
        for (NSDictionary *dic in result[@"data"]) {
            
            SongListTypeModel *model = [[SongListTypeModel alloc] initWithDataSource:dic];
            
            [self.dataSource addObject:model];
     
        }
        NSLog(@"%ld",_dataSource.count);
        [self.collectionView reloadData];
        
    } Failure:^(NSError *error) {
        
    }];
    
}

#pragma mark - ------------ 点击传值到上一页 ---------------
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SongListTypeModel *typeModel = self.dataSource[indexPath.section - 1];
    for (NSDictionary *dic in typeModel.children) {
        SongListTypeChlidrenModel *model = [[SongListTypeChlidrenModel alloc] initWithDataSource:dic];
        [_arrChildren addObject:model];
    }
    SongListTypeChlidrenModel *model = _arrChildren[indexPath.item];
    
    self.blockOfButtonType(model.tag);
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
